﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SmartIot.Models.SolarTerms;
using AutoMapper;
using SmartIot.MicroService.SolarTerms.Dto;

namespace SmartIot.MicroService.SolarTerms.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SolarTermsController : ControllerBase
    {
        public IMapper mapper { get; set; }

        public SolarTermsController(IMapper mapper)
        {
            this.mapper = mapper;
        }

        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <returns></returns>
        // GET api/SolarTerms/categorys
        [HttpGet("categorys")]
        public ActionResult<List<TermsCategoryDto>> GetCategorys()
        {
            var list = TermsCategory.FindAll();
            return mapper.Map<List<TermsCategoryDto>>(list);
        }

        /// <summary>
        /// 获取二十四节气
        /// </summary>
        /// <returns></returns>
        // GET api/SolarTerms/terms
        [HttpGet("terms")]
        public ActionResult<List<TermsDto>> GetSolarTerms()
        {
            var list = Terms.FindAll();
            return mapper.Map<List<TermsDto>>(list);
        }

        /// <summary>
        /// 根据节气ID和年份查询时间区间
        /// </summary>
        /// <param name="id"><see cref="Terms"/> id</param>
        /// <param name="year">年份</param>
        /// <returns></returns>
        // GET api/SolarTerms/term/time/5/2019
        [HttpGet("term/time/{id:int}/{year:int}")]
        public ActionResult<TermsTimeDto> GetTimeByTermAndYear(int id, int year)
        {
            var time = TermsTime.Find(TermsTime._.TermsID.Equal(id) & TermsTime._.Year.Equal(year));
            return mapper.Map<TermsTimeDto>(time);
        }

        /// <summary>
        /// 根据年份查询二十四节气时间区间
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/SolarTerms/term/time/2019
        [HttpGet("term/time/{id:int}")]
        public ActionResult<List<TermsTimeDto>> GetTimeByYear(int id)
        {
            var list = TermsTime.FindAll(TermsTime._.Year.Equal(id));
            return mapper.Map<List<TermsTimeDto>>(list);
        }

        /// <summary>
        /// 获取当前时间的节气
        /// </summary>
        /// <returns></returns>
        // GET api/SolarTerms/term/time/now
        [HttpGet("term/time/now")]
        public ActionResult<TermsTimeDto> GetNowTime()
        {
            var time = TermsTime.Find(TermsTime._.TermStartTime <= DateTime.Now);
            return mapper.Map<TermsTimeDto>(time);
        }

        /// <summary>
        /// 获取指定节气指定分类的信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="catId"></param>
        /// <returns></returns>
        // GET api/SolarTerms/article/5/1
        [HttpGet("article/{id}/{catId}")]
        public ActionResult<TermsArticleDto> GetArticle(int id, int catId)
        {
            var article = TermsArticle.Find(TermsArticle._.TermsID.Equal(id) & TermsArticle._.CategoryID.Equal(catId));
            return mapper.Map<TermsArticleDto>(article);
        }

        /// <summary>
        /// 根据节气ID获取图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/SolarTerms/album/5
        [HttpGet("album/{id}")]
        public ActionResult<List<TermsAlbumDto>> GetAlbum(int id)
        {
            var albums = TermsAlbum.FindAll(TermsAlbum._.TermsID.Equal(id));
            return mapper.Map<List<TermsAlbumDto>>(albums);
        }        
    }
}
